package com.java2007.hotel.controller.backend;


import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.controller.Controller;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.service.impl.FoodtypeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/22
 **/
@WebServlet("/foodtype")
public class FoodTypeController extends Controller {
    private FoodTypeService foodTypeService = new FoodtypeServiceImpl();

    /**
     * 通过foodTypeName查询，值为null则查询所有
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    public String findByTypeName(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取输入的值
        String keyword = request.getParameter("keyword");
        //查询typeName
        List<FoodType> foodTypeList = foodTypeService.findByTypeName(keyword);
        request.setAttribute("foodTypeList", foodTypeList);
        return "forward:/backend/detail/foodtype/food-type-list.jsp";
    }

    /**
     * 根据id删除
     *
     * @param request
     * @param response
     * @return 删除成功
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    public String deleteByTypeId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取输入的值
        String typeIdStr = request.getParameter("typeId");
        Integer typeId = Integer.valueOf(typeIdStr);
        foodTypeService.deleteByTypeId(typeId);
        return "redirect:/foodtype?method=findByTypeName&keyword= ";
    }

    /**
     * 通过id查询foodType信息并回显
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    public String findByTypeId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取输入的值
        String typeIdStr = request.getParameter("typeId");
        Integer typeId = Integer.valueOf(typeIdStr);
        //查询所有信息
        FoodType foodType = foodTypeService.findByTypeId(typeId);
        request.setAttribute("foodType", foodType);
        return "forward:/backend/detail/foodtype/food-type-update.jsp";

    }

    /**
     * 修改foodType
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String updateFoodType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取输入的值
        Map<String, String[]> parameterMap = request.getParameterMap();
        FoodType foodType = new FoodType();
        BeanUtils.populate(foodType, parameterMap);
        //判断重名
        int i = 0;
        List<FoodType> byTypeName = foodTypeService.findByTypeName(foodType.getTypeName().trim());
        for (FoodType type : byTypeName) {
            if (type.getTypeName().trim().equals(foodType.getTypeName().trim())){
                i = i + 1;
            }
        }
        if (i >= 2){
            return "名字重复";
        }


        Date date = new Date();
        foodType.setTypeUpdateTime(date);
        foodTypeService.updateFoodType(foodType);
        return "redirect:/foodtype?method=findByTypeName&keyword= ";
    }

    /**
     * 添加菜类别
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String addFoodType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取输入的值
        Map<String, String[]> parameterMap = request.getParameterMap();
        String typeName = request.getParameter("typeName").trim();

        //判断重名
        List<FoodType> byTypeName = foodTypeService.findByTypeName(typeName);
        for (FoodType foodType : byTypeName) {
            if (foodType.getTypeName().trim().equals(typeName)){
                return ServletConstant.jump("名字重复", "backend/detail/foodtype/food-type-save.jsp");
            }
        }

        if (!typeName.isEmpty() && !typeName.isEmpty()) {
            FoodType foodType = new FoodType();
            BeanUtils.populate(foodType, parameterMap);
            Date date = new Date();
            foodType.setTypeUpdateTime(date);
            foodTypeService.addFoodType(foodType);
            return "<script>\n" +
                    "  location.href = \"foodtype?method=findByTypeName&keyword= \";\n" +
                    "  alert(\"添加成功!\");\n" +
                    "</script>";
        }
        return "<script>\n" +
                "  alert(\"格式错误!\");\n" +
                "  location.href = \"backend/detail/foodtype/food-type-save.jsp\";\n" +
                "</script>";
    }
}




